import axios from 'axios'
import { Message } from 'element-ui'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

service.interceptors.response.use(
  response => {
    const res = response.data

    if (res.code !== 0) {
      Message({
        message: res.message,
        type: 'error',
        duration: 5 * 1000
      })

      return Promise.reject(response)
    } else {
      return response.data
    }
  },
  error => {
    console.log(error.message)
    Message({
      message: '请求失败!',
      type: 'error',
      duration: 5 * 1000
    })

    return Promise.reject(error)
  })

export default service
